package Stack;

import org.junit.Test;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Stack
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-21  09:52
 * @Description: TODO
 * @Version: 1.0
 */
public class 每日温度739 {

    // 使用的是单调栈的方式 栈中存储的值不是实际的值 而是实际的下标，可以通过的数组下标来实现访问的是元素的实际的值
    public int[] dailyTemperatures(int[] temperatures) {
        Deque<Integer> stack=new ArrayDeque<>();
        int[] res=new int[temperatures.length];
        for (int i=0;i<temperatures.length;i++){
            // 单调栈的相关模板
            while (!stack.isEmpty()&&temperatures[stack.peek()]<temperatures[i]){
                int index=stack.poll();
                res[index]=i-index;
            }
            stack.push(i);
        }
        return res;
    }

    public int[] dailyTemperatures3(int[] temperatures) {
        int[] result=new int[temperatures.length];
        // 设置单调栈
        Deque<Integer> stack=new LinkedList<>();
        for (int i=0;i<temperatures.length;i++){
            while (!stack.isEmpty()&&temperatures[i]>temperatures[stack.peek()]){
                // 弹出栈
                int index=stack.pop();
                result[index]=i-index;
            }
            // 存储下标
            stack.push(i);
        }
        return result;
    }


    // 属于暴力解法
    public int[] dailyTemperatures2(int[] temperatures) {
        int[] result=new int[temperatures.length];
        for (int i=0;i<temperatures.length;i++){
            int count=0;
            int j=i+1;
            while (j<temperatures.length){
                if (temperatures[j]>temperatures[i]){
                    result[i]=count+1;
                    break;
                }
                count++;
                j++;
            }
        }
        return result;
    }

    @Test
    public void test(){
        int[] ints = dailyTemperatures(new int[]{30,60,90});
        System.out.println(Arrays.toString(ints));
    }
}
